package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.api.shopapi.entity.MedicationGroup;
import com.api.shopapi.entity.MedicationItem;
import com.api.shopapi.entity.dto.MedicationGroupDTO;
import com.api.shopapi.mapper.MedicationGroupMapper;
import com.api.shopapi.service.MedicationGroupService;
import com.api.shopapi.service.MedicationItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("MedicationGroupServiceImplOne")
public class MedicationGroupServiceImpl implements MedicationGroupService {

    @Autowired
    private MedicationGroupMapper medicationGroupMapper;

    @Autowired
    @Qualifier("MedicationItemServiceImplOne")
    private MedicationItemService medicationItemService;

    @Override
    @Transactional
    public int insert(MedicationGroup medicationGroup, JSONArray itemNameArr) {
        String medicationGroupCode = medicationGroup.getMedicationGroupCode();
        //插入项
        insertMedicationItem(medicationGroupCode,itemNameArr);

        return medicationGroupMapper.insert(medicationGroup);
    }

    private void insertMedicationItem(String medicationGroupCode, JSONArray itemNameArr) {
        MedicationItem medicationItem = new MedicationItem();
        for (Object i :itemNameArr){
            String itemName = (String) i;
            medicationItem.setMedicationItemCode(UUID.randomUUID().toString());
            medicationItem.setMedicationGroupCode(medicationGroupCode);
            medicationItem.setName(itemName);
            medicationItemService.insert(medicationItem);
        }
    }

    @Override
    @Transactional
    public int updateByMedicationGroupCode(MedicationGroup medicationGroup, JSONArray itemNameArr) {
        String medicationGroupCode = medicationGroup.getMedicationGroupCode();
        //删除原来的项
        medicationItemService.delByMedicationGroupCode(medicationGroupCode);
        //插入新的项
        insertMedicationItem(medicationGroupCode,itemNameArr);

        return medicationGroupMapper.updateByMedicationGroupCode(medicationGroup);
    }

    @Override
    public MedicationGroup getByElderlyCode(String elderlyCode) {
        return medicationGroupMapper.getByElderlyCode(elderlyCode);
    }

    @Override
    public MedicationGroup getByMedicationGroupCode(String medicationGroupCode) {
        return medicationGroupMapper.getByMedicationGroupCode(medicationGroupCode);
    }

    @Override
    public PageInfo<MedicationGroupDTO> getPageList(int pageIndex, int pageSize, String institutionCode, int status, String elderlyName) {
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicationGroupDTO> medicationGroupDTOList = medicationGroupMapper.getPageList(institutionCode,status,elderlyName);
        PageInfo<MedicationGroupDTO> pageInfo = new PageInfo<>(medicationGroupDTOList);
        return pageInfo;
    }


    @Override
    public
    PageInfo<MedicationGroup> pageByGovCode(int pageIndex, int pageSize, String governmentCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicationGroup> medicationGroupDTOList = medicationGroupMapper.listByGovCode(governmentCode);
        PageInfo<MedicationGroup> pageInfo = new PageInfo<>(medicationGroupDTOList);
        return pageInfo;
    }

}
